package com.yuan.controller;


import com.yuan.Result.Result;
import com.yuan.entity.Dto.EmpLoginDto;
import com.yuan.entity.Emp;
import com.yuan.service.IEmpService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-11-05
 */
@RestController
@RequestMapping
@Slf4j
public class EmpController {

    @Autowired
    private IEmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody EmpLoginDto empLoginDto) {
        log.info("登陆：{}", empLoginDto);
        Emp emp = empService.login(empLoginDto);
        Map<String, Object> claims = new HashMap<>();
        claims.put("EMP_ID", emp.getId());
        claims.put("USERNAME", emp.getUsername());
        String token = Jwts.builder()
                .setHeaderParam("typ", "JWT") //令牌类型
                .setHeaderParam("alg", "HS256") //签名算法
                .setExpiration(new Date(System.currentTimeMillis() + 3600 * 10000))
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256, "atguigu123")
                .compact();

        return Result.success(token);
    }

}
